var db = require('../../config/db')
var jwt = require('jsonwebtoken');
var config = require('../../schema/config')//导入密钥配置文件
exports.login = (req,res)=>{
    var admininfo = req.body
    var sql = `select * from admin_info where name=?`
    db.query(sql, admininfo.name, (err, results)=>{
        if(err) return res.cc('500', err)
        if(results.length !== 1) return res.cc('404', '没有该用户')
        if(results[0].password !== admininfo.password) return res.cc('404', '密码错误')
        //生成token
        var admin = {...results[0], password:'', phone: '',avatar:''}//把里边的某些键值清空
        
        //加密
        var tokenStr = jwt.sign(admin, config.jwtSecrectKey, {expiresIn: config.expiresIn})//加密对象，加密解密的密钥，有效期
        res.send({ //返回前端信息
            status: 200,
            message: '登录成功',
            token: 'Bearer '+tokenStr,//方便前端解析
        })
    })
}
